<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams">
      <!-- 新增等操作按钮 -->
      <template #actionMore>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['countryside:report:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </template>
    </Search>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <Table
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
    >
      <template #rating="{ row }">
        <el-rate v-model="row.rating" disabled text-color="#ff9900" />
      </template>
      <template #result="{ row }">
        <div v-if="!row.result">--</div>
        <dict-tag v-else :type="DICT_TYPE.SYSTEM_CUNRESULT_TYPE" :value="row.resultStatus" />
      </template>
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm(row.id)"
          v-hasPermi="['country:report:query']"
        >
          详情
        </el-button>
        <el-button
          v-if="row.status == 1"
          link
          type="primary"
          v-hasPermi="['country:report:careOf']"
          @click="handleCareOf(row.id)"
        >
          转交事件
        </el-button>
        <el-button
          v-if="row.status == 2 && row.userId == userId"
          link
          type="primary"
          v-hasPermi="['country:report:conclude']"
          @click="handleConclude(row.id)"
        >
          办结
        </el-button>
        <el-button
          link
          type="danger"
          v-hasPermi="['country:report:delete']"
          @click="handleDelete(row.id)"
        >
          删除
        </el-button>
      </template>
    </Table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ReportForm ref="formRef" @success="getList" />
  <deliverTo ref="deliverToRef" @success="getList" />
  <conclude ref="concludeRef" @success="getList" />
</template>
<script setup lang="ts" name="Report">
import conclude from './conclude.vue'
import deliverTo from './deliverTo.vue'
import { allSchemas } from './report.data'
import * as ReportApi from '@/api/countryside/report'
import ReportForm from './ReportForm.vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/store/modules/user'
import { DICT_TYPE } from '@/utils/dict'
const { t } = useI18n()
const userStore = useUserStore()
const message = useMessage()
const { push } = useRouter()
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
// 详细可见：https://doc.crc.cn/vue3/crud-schema/
const { tableObject, tableMethods } = useTable({
  getListApi: ReportApi.getReportPage, // 分页接口
  delListApi: ReportApi.deleteReport // 删除接口
})
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods
const userId = computed(() => userStore.user.id)
/** 添加/修改操作 */
const formRef = ref()
const deliverToRef = ref()
const concludeRef = ref()
const openForm = (id?: number) => {
  push('/countryside/report/details/' + id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  ElMessageBox.confirm('是否删除该条数据', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await ReportApi.deleteReport(id)
      message.success(t('common.delSuccess'))
      getList()
    })
    .catch(() => {})
}
/**
 * @description:办结事件
 * @param {*} id
 * @return {*}
 */
const handleConclude = (id) => {
  concludeRef.value.open(id)
}
/**
 * @description: 转交事件
 * @return {*}
 */
function handleCareOf(id) {
  deliverToRef.value.open(id)
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
